// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.

//resourceSpecification数据表定义
package dao

import (
	"fleetmanager/db/dbm"
	"time"
)

type ResourceSpecification struct {
	InstanceType  string    `orm:"column(instance_type);size(16);" json:"instance_type"`
	Id            string    `orm:"column(id);size(64);pk" json:"id"`
	ProjectId     string    `orm:"column(project_id);size(64)" json:"project_id"`
	Name          string    `orm:"column(name);type(text);size(1024);null" json:"name"`
	Specification string    `orm:"column(specification);type(text);size(1024);null" json:"specification"`
	Flavor        string    `orm:"column(flavor);type(text);size(1024);null" json:"flavor"`
	Region        string    `orm:"column(region);size(64);type(text)" json:"region"`
	CreationTime  time.Time `orm:"column(creation_time);type(datetime);auto_now_add" json:"creation_time"`
	UpdateTime    time.Time `orm:"column(update_time);type(datetime);auto_now" json:"update_time"`
}

func (r *ResourceSpecification) TableIndex() [][]string {
	return [][]string{
		{"ProjectId"},
	}
}

type resourceSpecificationStorage struct{}

var rss = resourceSpecificationStorage{}

// GetFleetStorage 获取resourceSpecification存储对象
func GetResourceSpecificationStorage() *resourceSpecificationStorage {
	return &rss
}

// Insert 插入resourceSpecification
func (s *resourceSpecificationStorage) Insert(r *ResourceSpecification) error {
	_, err := dbm.Ormer.Insert(r)
	return err
}

// Update 更新resourceSpecification
func (s *resourceSpecificationStorage) Update(r *ResourceSpecification, cols ...string) error {
	_, err := dbm.Ormer.Update(r, cols...)

	return err
}

// Get 获取resourceSpecification详情
func (s *resourceSpecificationStorage) Get(f Filters) (*ResourceSpecification, error) {
	var r ResourceSpecification
	err := f.Filter(ResourceSpecificationTable).One(&r)
	if err != nil {
		return nil, err
	}

	return &r, err
}

// Get 获取resourceSpecificationList列表
func (s *resourceSpecificationStorage) List(f Filters, offset int, limit int) ([]ResourceSpecification, error) {
	var r []ResourceSpecification
	_, err := f.Filter(ResourceSpecificationTable).Offset(offset).Limit(limit).All(&r)
	return r, err
}

// Delete 删除resourceSpecification
func (s *resourceSpecificationStorage) Delete(r *ResourceSpecification) error {
	_, err := dbm.Ormer.Delete(r)

	return err
}
